﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SYNANLib;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace OffLineFilter
{
    public static class SyntaxExtensions
    {
        public static IEnumerable<IWord> GetWords(this ISentence sentence)
        {
            IWord[] words = new SYNANLib.Word[sentence.WordsNum];
            for (int i = 0; i < sentence.WordsNum; i++)
                words[i] = sentence.Word[i];
            return words;
        }

        public static IEnumerable<IHomonym> GetHomonyms(this IWord word)
        {
            IHomonym[] homonyms = new Homonym[word.HomonymsCount];
            for (int i = 0; i < word.HomonymsCount; i++)
                homonyms[i] = word.Homonym[i];
            return homonyms;
        }
    }

    public static class StatisticsExtensions
    {
        public static double Variance(this IEnumerable<int> source)
        {
            double avg = source.Average();
            int count = source.Count();
            double squares = source
                .Select(element => Math.Pow(element - avg, 2))
                .Sum();
            return squares / count;
        }

        public static bool IsPunctuation(this string s)
        {
            return Regex.IsMatch(s, @"\p{P}");
        }

        public static T GetValueByKey<T>(this Dictionary<String, T> dictionary, String key)
        {
            return (dictionary.ContainsKey(key)) ? dictionary[key] : default(T);
        }
    }

    public static class Md5Extensions
    {
        public static Guid ComputeHash(this MD5 md5, string str)
        {
            byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(str));
            return new Guid(hash);
        }
    }
}
